﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Bitirme.DAL.Utils
{
    public static class DateUtils
    {
        public static DateTime? TransformTextToDateTime(string textDate)
        {
         int day, month, year;
         Int32.TryParse(textDate.Substring(0, 2), out day);
            Int32.TryParse(textDate.Substring(3, 2), out month);
            Int32.TryParse(textDate.Substring(6, 4), out year);

            DateTime? foundationDate = new DateTime(year,month,day);
            return foundationDate;
        }

        public static String TransformDateTimeToText(DateTime? date)
        {
            return String.Format("{0:dd-MM-yyyy}",date);
        }

        public static DateTime? SynchronizeServerDateToLocal(DateTime? date)
        {
            DateTime? localDate = null;
            // add the time difference between sql server and local to the insert date taken from sql server
            TimeSpan dateDifference = new TimeSpan(8, 56, 46);
            localDate = date.Value.Add(dateDifference);
            return localDate;
        }

        /// <summary>
        /// Populates a drop down list with years from a down limit to current year 
        /// </summary>
        /// <param name="downLimit">Down limit of years</param>
        /// <returns>A list containing years in string format</returns>
        public static List<String> PopulateYearsDDL(int downLimit)
        {
            List<String> years = new List<String>();
            int upLimit = DateTime.Now.Year;

            for (int i = downLimit; i <= upLimit; i++)
            {
                years.Add(i.ToString());
            }
            return years;
        }

        /// <summary>
        /// Returns date in "x hours,days,weeks ago" format
        /// </summary>
        /// <param name="d">Nullable date to be formatted</param>
        /// <returns>Formatted date string</returns>
        public static String GetPrettyDate(DateTime? d)
        {
            // 1.
            // Get time span elapsed since the date.
            TimeSpan s = DateTime.Now.Subtract(d.Value);

            // 2.
            // Get total number of days elapsed.
            int dayDiff = (int)s.TotalDays;

            // 3.
            // Get total number of seconds elapsed.
            int secDiff = (int)s.TotalSeconds;

            // 4.
            // Don't allow out of range values.
            if (dayDiff < 0 || dayDiff >= 31)
            {
                return null;
            }

            // 5.
            // Handle same-day times.
            if (dayDiff == 0)
            {
                // A.
                // Less than one minute ago.
                if (secDiff < 60)
                {
                    return "az önce";
                }
                // B.
                // Less than 2 minutes ago.
                if (secDiff < 120)
                {
                    return "1 dakika önce";
                }
                // C.
                // Less than one hour ago.
                if (secDiff < 3600)
                {
                    return string.Format("{0} dakika önce",
                        Math.Floor((double)secDiff / 60));
                }
                // D.
                // Less than 2 hours ago.
                if (secDiff < 7200)
                {
                    return "1 saat önce";
                }
                // E.
                // Less than one day ago.
                if (secDiff < 86400)
                {
                    return string.Format("{0} saat önce",
                        Math.Floor((double)secDiff / 3600));
                }
            }
            // 6.
            // Handle previous days.
            if (dayDiff == 1)
            {
                return "dün";
            }
            if (dayDiff < 7)
            {
                return string.Format("{0} gün önce",
                    dayDiff);
            }
            if (dayDiff < 31)
            {
                return string.Format("{0} hafta önce",
                    Math.Ceiling((double)dayDiff / 7));
            }
            return null;
        }
    }
}